
package com.ttomato.game.glbasics;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;

import javax.microedition.khronos.opengles.GL10;

import com.ttomato.game.framework.Game;
import com.ttomato.game.framework.Screen;
import com.ttomato.game.framework.gl.Texture;
import com.ttomato.game.framework.gl.Vertices;
import com.ttomato.game.framework.impl.GLGame;
import com.ttomato.game.framework.impl.GLGraphics;

public class TextureTriangleTest extends GLGame {

    @Override
    public Screen getStartScreen() {
        // TODO Auto-generated method stub
        return new TextureTriangleScreen(this);
    }

    class TextureTriangleScreen extends Screen {

        final static int VERTEX_SIZE = (2 + 2) * 4;

        GLGraphics mGlGraphics;

        Texture mTexture;

        Vertices mVertices2;

        public TextureTriangleScreen(Game game) {
            super(game);
            mGlGraphics = getGLGraphics();

            mTexture = new Texture((GLGame) mGame, "newbobrgb888.png");

            mVertices2 = new Vertices(mGlGraphics, 4, 6, false, true);

            mVertices2.setVertices(new float[] {
                    100.0f, 100.0f, 0.0f, 1.0f,
                    228.0f, 100.0f, 1.0f, 1.0f,
                    228.0f, 228.0f, 1.0f, 0.0f,
                    100.0f, 228.0f, 0.0f, 0.0f
            }, 0, 16);
            mVertices2.setIndices(new short[] {
                    0, 1, 2,
                    2, 3, 0
            }, 0, 6);
        }

        @Override
        public void update(float deltaTime) {
            mGame.getInput().getTouchEvents();
            mGame.getInput().getKeyEvents();
        }

        @Override
        public void present(float deltaTime) {
            GL10 gl = mGlGraphics.getGl();
            gl.glViewport(0, 0, mGlGraphics.getWidth(),
                    mGlGraphics.getHeight());
            gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
            gl.glMatrixMode(GL10.GL_PROJECTION);
            gl.glLoadIdentity();
            gl.glOrthof(0, 320, 0, 480, 1, -1);

            gl.glEnable(GL10.GL_TEXTURE_2D);
            mTexture.bind();
            mVertices2.draw(GL10.GL_TRIANGLES, 0, 6);

            // gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
            // gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
            //
            // mVertices.position(0);
            // gl.glVertexPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, mVertices);
            // mVertices.position(2);
            // gl.glTexCoordPointer(2, GL10.GL_FLOAT, VERTEX_SIZE, mVertices);
            //
            // gl.glDrawElements(GL10.GL_TRIANGLES, 6, GL10.GL_UNSIGNED_SHORT,
            // mIndices);

        }

        @Override
        public void pause() {

        }

        @Override
        public void resume() {

        }

        @Override
        public void dispose() {

        }
    }

}
